const app = getApp();
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collectData: [],//收藏的展商id数组
    logo: '/static/settings/logo.png',
  },
  
  //获取展商数据
  getExhibitorData: function () {
    let that = this;
    that.setData({
      loading: true
    })
    //加载信息
    util.request(api.EXHIBITOR_DETAIL+'/'+that.data.id).then(function (res) {
      
      if (res.success === true) {
        let data = new Object();
        data.exhibitor = res.result;
        that.setData(data);
      }
    });

  },

  //收藏/取消收藏
  collect: function (e) {
    let id = e.currentTarget.dataset.id;
    let that = this;
    let arr = that.data.collectData;
    let index = arr.findIndex(item => item === id);
    if (index === -1) {
      //不存在 ---收藏
      arr.push(id)
      util.request(api.EXHIBITOR_COLLECT + '/' + id).then(function (res) {

      });
    } else {
      //存在  ---取消收藏
      arr.splice(index, 1)
      util.request(api.EXHIBITOR_COLLECT_CANCEL + '/' + id).then(function (res) {

      });
    }
    that.setData({
      'collectData': arr
    })
  },
  
  //获取用户收藏信息
  getCollect() {
    let that = this;
    util.request(api.EXHIBITOR_GET_COLLECT).then(function (res) {
      if (res.success === true) {
        let data = new Object();
        data.collectData = res.result;
        that.setData(data);
      }
    });
  },
  //打开展位图
  openHallImage(){
    let standNo = this.data.exhibitor.standNo;
    if(!standNo){
      util.$Message({
        content: '展位号未填写',
        type: 'error'
      });
      return;
    }
    let hallNo = standNo.substring(0, 2);
    if (hallNo.length===0) {
      util.$Message({
        content: '展位号未填写',
        type: 'error'
      });
      return;
    }
    util.request(api.HALL_GET_IMAGE+'/'+hallNo).then(function (res) {
      if (res.success === true) {
        
        let image = res.result;
        if (image && image.length > 0) {
          wx.previewImage({
            current: '',
            urls: [image]
          })
        } else {
          util.$Message({
            content: '此展馆暂未开放展位图',
            type: 'error'
          });
        }

      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData(options)
    wx.getSystemInfo({
      success(res) {
        that.setData({ wh: res.windowHeight })
      }
    })
    this.getExhibitorData();
  }
})